package com.example.baselibs.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.example.baselibs.R;

public abstract class BaseService extends Service {
    public static final int KEEP_SERVICE=0x11;
    public BaseService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        createChannel();
        //API 18以下，直接发送Notification并将其置为前台
        if (Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN_MR2){
            startForeground(KEEP_SERVICE,new Notification());
        }else {
            //API 18以上，发送Notification并将其置为前台后，启动InnerService
            NotificationCompat.Builder builder= null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                builder = new NotificationCompat.Builder(this,"WXB");
            }
            builder.setSmallIcon(R.mipmap.ic_longma_logo);
            startForeground(KEEP_SERVICE,builder.build());
            startService(new Intent(this,InnerService.class));
            Log.i("Service","启动InnerService");
        }

        initCreate();

    }

    /**
     * 在设置开启服务通知之前，一定要先创建一个通知渠道，而且两个服务所用的都是同一个渠道
     */
    public void createChannel(){
        //这里要注意一点：channelid必须要一致，否者服务还是一样会被杀死
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = mNotificationManager.getNotificationChannel("WXB");
            if (notificationChannel ==null) {
                NotificationChannel channel =new NotificationChannel("WXB",
                        "com.example.baselibs.service", NotificationManager.IMPORTANCE_MIN);

                //是否在桌面icon右上角展示小红点
                channel.enableLights(true);

                //小红点颜色
                channel.setLightColor(Color.RED);

                //通知显示
                channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

                //是否在久按桌面图标时显示此渠道的通知
                //channel.setShowBadge(true);

                mNotificationManager.createNotificationChannel(channel);

            }

        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        initData(intent,flags,startId);
        return START_REDELIVER_INTENT;
    }
    abstract void initData(Intent intent, int flags, int startId);
    abstract void initCreate();

    public class InnerService extends Service{

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            useStartForeground();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    stopForeground(true);
                    NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        manager.deleteNotificationChannel("WXB");//关闭Service前台通知渠道
                    }
                    stopSelf();
                    Log.i("Service","关闭通知，关闭InnerService");
                }
            },100);
        }

        public void useStartForeground(){

            NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this, "WXB");

            mBuilder.setSmallIcon(R.mipmap.ic_longma_logo);

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {

                mBuilder.setContentTitle(getResources().getString(R.string.app_name));

            }

            //当id设置为0时，隐藏不显示通知，那么服务在60s后一样时会被杀死的。

            //要如何隐藏通知而服务不被杀死，这个还在学习中。

            startForeground(KEEP_SERVICE,mBuilder.build());
        }
    }
}
